package sq.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import sq.dao.*;
import sq.service.OrdersService;
import sq.vo.BaseRespVO;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: sunquan
 * @Date: 2018/3/30
 * @Time: 14:00
 */
@RestController
public class OrdersController {
    @Autowired
    private OrdersService ordersService;
    /**
     * 选中菜品
     */
    @RequestMapping(value = "/cart/checked", method = RequestMethod.POST)
    @ResponseBody
    public BaseRespVO queryCheckedCart(String checkedId) {
        BaseRespVO baseRespVO = new BaseRespVO();
        ArrayList<Cart> list = (ArrayList<Cart>) ordersService.findCheckedCart(checkedId);
        if (list.isEmpty()) {
            baseRespVO.setCode("1");
            baseRespVO.setMsg("没有文件查询失败");
        } else {
            baseRespVO.setCode("0");
            baseRespVO.setData(list);
            baseRespVO.setMsg("查询成功");
        }
        return baseRespVO;

    }
    /**
     * 加入订单
     */
    @RequestMapping(value = "/orders/add", method = RequestMethod.POST)
    @ResponseBody
    public BaseRespVO addOrders(String checkedId,double price,HttpServletRequest request) {
        BaseRespVO baseRespVO = new BaseRespVO();
        ordersService.ordersAdd(checkedId,price,request);
        baseRespVO.setCode("0");
        baseRespVO.setMsg("保存成功");
        return baseRespVO;
    }

    /**
     * 查询订单
     */
    @RequestMapping(value = "/orders", method = RequestMethod.POST)
    @ResponseBody
    public BaseRespVO findOrders(HttpServletRequest request) {
        BaseRespVO baseRespVO = new BaseRespVO();
        ArrayList<Orders> ordersList = null;
        ordersList = (ArrayList<Orders>) ordersService.findOrders(request);
        baseRespVO.setCode("0");
        baseRespVO.setMsg("查询成功");
        baseRespVO.setData(ordersList);
        return baseRespVO;
    }
    /**
     * 查询所有订单
     */
    @RequestMapping(value = "/orders/all", method = RequestMethod.POST)
    @ResponseBody
    public BaseRespVO findAllOrders() {
        BaseRespVO baseRespVO = new BaseRespVO();
        ArrayList<Orders> ordersList = null;
        ordersList = (ArrayList<Orders>) ordersService.findAllOrders();
        baseRespVO.setCode("0");
        baseRespVO.setMsg("查询成功");
        baseRespVO.setData(ordersList);
        return baseRespVO;
    }

    /**
     * 查询订单详情
     */
    @RequestMapping(value = "/ordersItem/", method = RequestMethod.POST)
    @ResponseBody
    public BaseRespVO findOrdersItem(Integer ordersId) {
        BaseRespVO baseRespVO = new BaseRespVO();
        ArrayList<OrdersItem> ordersItemList = null;
        ordersItemList = (ArrayList<OrdersItem>) ordersService.findOrdersItem(ordersId);
        baseRespVO.setCode("0");
        baseRespVO.setMsg("查询成功");
        baseRespVO.setData(ordersItemList);
        return baseRespVO;
    }

    /**
     * 接单
     */
    @RequestMapping(value = "/orders/receive", method = RequestMethod.POST)
    @ResponseBody
    public BaseRespVO receiveOrders(Integer ordersId) {
        BaseRespVO baseRespVO = new BaseRespVO();
        ordersService.receive(ordersId);
        baseRespVO.setCode("0");
        baseRespVO.setMsg("接单成功");
        return baseRespVO;
    }

    /**
     * 配送
     */
    @RequestMapping(value = "/orders/take", method = RequestMethod.POST)
    @ResponseBody
    public BaseRespVO findStatus(Integer ordersId) {
        BaseRespVO baseRespVO = new BaseRespVO();
        ordersService.toReceiveStatus(ordersId);
        baseRespVO.setCode("0");
        baseRespVO.setMsg("更改订单状态成功");
        return baseRespVO;
    }

    /**
     * 完成
     */
    @RequestMapping(value = "/orders/complete", method = RequestMethod.POST)
    @ResponseBody
    public BaseRespVO completeOrders(Integer ordersId) {
        BaseRespVO baseRespVO = new BaseRespVO();
        ordersService.complete(ordersId);
        baseRespVO.setCode("0");
        baseRespVO.setMsg("更改订单状态成功");
        return baseRespVO;
    }
    /**
     * 查询已完成订单
     */
    @RequestMapping(value = "/orders/final", method = RequestMethod.POST)
    @ResponseBody
    public BaseRespVO findCompleteOrders() {
        BaseRespVO baseRespVO = new BaseRespVO();
        ArrayList<Orders> orders= (ArrayList<Orders>) ordersService.findFinalOrders();
        baseRespVO.setCode("0");
        baseRespVO.setData(orders);
        baseRespVO.setMsg("查询完成订单成功");
        return baseRespVO;
    }

    @RequestMapping(value = "/data/totalSell", method = RequestMethod.POST)
    @ResponseBody
    public BaseRespVO showTotalSell (@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date begin,
                                           @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date end) {
        ArrayList<CountStatistics> totalSellInfo = (ArrayList<CountStatistics>)ordersService.findTotalSell(begin,end);
        BaseRespVO<ArrayList> sellInfoRespVO = new BaseRespVO<ArrayList>();
        if (totalSellInfo == null) {
            sellInfoRespVO.setCode("1");
            sellInfoRespVO.setData(null);
        } else {
            sellInfoRespVO.setCode("0");
            sellInfoRespVO.setData((ArrayList) totalSellInfo);
        }
        return sellInfoRespVO;
    }

    @RequestMapping(value = "/data/top", method = RequestMethod.POST)
    @ResponseBody
    public BaseRespVO findTop(@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date begin,
                                     @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date end) {
        ArrayList<CountStatistics> topInfo = (ArrayList<CountStatistics>)ordersService.findTop(begin,end);
        BaseRespVO<ArrayList> topInfoRespVO = new BaseRespVO<ArrayList>();
        if (topInfo == null) {
            topInfoRespVO.setCode("1");
            topInfoRespVO.setData(null);
        } else {
            topInfoRespVO.setCode("0");
            topInfoRespVO.setData((ArrayList) topInfo);
        }
        return topInfoRespVO;
    }

    @RequestMapping(value = "/orders/item/top", method = RequestMethod.POST)
    @ResponseBody
    public BaseRespVO findqtTop() {
        ArrayList<CountStatistics> topInfo = (ArrayList<CountStatistics>)ordersService.findqtTop();
        BaseRespVO<ArrayList> topInfoRespVO = new BaseRespVO<ArrayList>();
        if (topInfo == null) {
            topInfoRespVO.setCode("1");
            topInfoRespVO.setData(null);
        } else {
            topInfoRespVO.setCode("0");
            topInfoRespVO.setData((ArrayList) topInfo);
        }
        return topInfoRespVO;
    }

}
